﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Model;
using Hangjing.SQLServerDAL;

using System.Text;
using System.Text.RegularExpressions;
using Hangjing.SQLServerDAL.serverinterface;
using Newtonsoft.Json;
using Hangjing.BaiduMapServerAPI;
using Hangjing.Common;
using System.Threading.Tasks;

/// <summary>
/// 上传坐标
/// </summary>
public partial class AndroidAPI_Deliver_uploadlatlng : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();

        string did = WebUtility.InputText(Request["did"]);
        string lat = WebUtility.InputText(Request["lat"]);
        string lng = WebUtility.InputText(Request["lng"]);
        string speed = WebUtility.InputText(Request["speed"]);
        string direction = WebUtility.InputText(Request["direction"]);


        apiResultInfo res = new apiResultInfo()
        {
            msg = "上传成功",
            state = 1,
        };

      
        if (lat == "0" || lat.Length == 0)
        {
            res.msg = "坐标没有";
            Response.Write(JsonConvert.SerializeObject(res));
            Response.End();

            return;
        }


     
        BaiduMap map = new BaiduMap(lat, lng);
        HttpItem httpdata= map.convertLoacation();

        Task.Run(() => {
            HttpHelper objhttp = new HttpHelper();
            string returnmsg = objhttp.GetHtml(httpdata);
            if (returnmsg == objhttp.ErrorCode)
            {
                res.msg = "转化失败";
                Response.Write(JsonConvert.SerializeObject(res));
                Response.End();
                return;
            }
            else
            {
                BaiduMapInfo mapdata = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<BaiduMapInfo>(returnmsg);
                if (mapdata.status == "0")
                {
                    string jsonstr = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(mapdata.result);
                    IList<LocationInfo> rs = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<IList<LocationInfo>>(jsonstr);
                    if (rs != null & rs.Count > 0)
                    {
                        lat = rs[0].y;
                        lng = rs[0].x;

                        new GPS_Records().Add(new GPS_RecordsInfo()
                        {
                            JH1 = did,
                            JH2 = lng,
                            JH3 = lat,
                            JH4 = speed,
                            JH5 = direction,
                            AddTime = DateTime.Now,
                            AddName = "",
                            UpTime = DateTime.Now,
                            Remark = "",
                            Del = 0,
                            baidu = 0
                        });


                        Response.Write(JsonConvert.SerializeObject(res));
                        Response.End();
                    }
                }

            }



        });

        Response.Write(JsonConvert.SerializeObject(res));
        Response.End();
        return;



    }
}
